package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.Yearqs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.VideoInfoMapper;
import com.ruoyi.system.domain.VideoInfo;
import com.ruoyi.system.service.IVideoInfoService;

/**
 * 视频后台管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-16
 */
@Service
public class VideoInfoServiceImpl implements IVideoInfoService {
    @Autowired
    private VideoInfoMapper videoInfoMapper;

    /**
     * 查询视频后台管理
     *
     * @param id 视频后台管理主键
     * @return 视频后台管理
     */
    @Override
    public VideoInfo selectVideoInfoById(Long id) {
        return videoInfoMapper.selectVideoInfoById(id);
    }

    /**
     * 查询视频后台管理列表
     *
     * @param videoInfo 视频后台管理
     * @return 视频后台管理
     */
    @Override
    public List<VideoInfo> selectVideoInfoList(VideoInfo videoInfo) {
        return videoInfoMapper.selectVideoInfoList(videoInfo);
    }

    @Override
    public List<Yearqs> selectYearqsList() {

        List<VideoInfo> selectVideoInfoList = videoInfoMapper.selectVideoInfoList(null);

        List<Yearqs> yearqsList = new ArrayList<>();

        HashSet<Integer> years = new HashSet<>();
        //获取年
        selectVideoInfoList.forEach(videoInfo -> {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(videoInfo.getVideoDay());
            Integer year = calendar.get(Calendar.YEAR);
            years.add(year);
        });
        //获取期数
        years.forEach(y -> {
            Yearqs yearqs = new Yearqs();
            yearqs.setYear(y);
            List<VideoInfo> qs = new ArrayList<>();
            selectVideoInfoList.forEach(videoInfo -> {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(videoInfo.getVideoDay());
                Integer year = calendar.get(Calendar.YEAR);
                if (y.equals(year)) {
                    qs.add(videoInfo);
                }
            });
            //期数排序
            qs.sort(Comparator.comparingInt(a -> a.getVideoQs().intValue()));
            yearqs.setVideoInfoList(qs);
            yearqsList.add(yearqs);
        });
        return yearqsList;
    }

    /**
     * 新增视频后台管理
     *
     * @param videoInfo 视频后台管理
     * @return 结果
     */
    @Override
    public int insertVideoInfo(VideoInfo videoInfo) {
        videoInfo.setCreateTime(DateUtils.getNowDate());
        return videoInfoMapper.insertVideoInfo(videoInfo);
    }

    /**
     * 修改视频后台管理
     *
     * @param videoInfo 视频后台管理
     * @return 结果
     */
    @Override
    public int updateVideoInfo(VideoInfo videoInfo) {
        videoInfo.setUpdateTime(DateUtils.getNowDate());
        return videoInfoMapper.updateVideoInfo(videoInfo);
    }

    /**
     * 批量删除视频后台管理
     *
     * @param ids 需要删除的视频后台管理主键
     * @return 结果
     */
    @Override
    public int deleteVideoInfoByIds(Long[] ids) {
        return videoInfoMapper.deleteVideoInfoByIds(ids);
    }

    /**
     * 删除视频后台管理信息
     *
     * @param id 视频后台管理主键
     * @return 结果
     */
    @Override
    public int deleteVideoInfoById(Long id) {
        return videoInfoMapper.deleteVideoInfoById(id);
    }
}
